import pathlib
from fastapi import APIRouter

router = APIRouter()
@router.get("/get_all_workflows",description="获取工作流名称",summary="获取所有工作流")
def get_all_workflows():
    """
    获取所有工作流名称
    工作流是工作目录下data文件夹下的所有json文件
    工作流名称为文件名
    工作流内容为json文件内容
    """
    workflows = []
    for file in pathlib.Path("data").glob("*.json"):
        workflows.append(file.stem)
    return workflows

@router.post("/create_workflow",description="创建工作流",summary="创建工作流")
def create_workflow(name: str, content: str):
    """
    创建工作流
    """
    with open(f"data/{name}.json", "w") as f:
        f.write(content)
    return {"message": "success"}

@router.get("/get_workflow",description="获取工作流",summary="获取工作流")
def get_workflow(name: str):
    """
    获取工作流
    """
    with open(f"data/{name}.json", "r") as f:
        content = f.read()
    return {"name": name, "content": content}

@router.post("/update_workflow",description="更新工作流",summary="更新工作流")
def update_workflow(name: str, content: str):
    """
    更新工作流
    """
    with open(f"data/{name}.json", "w") as f:
        f.write(content)
    return {"message": "success"}

@router.post("/delete_workflow",description="删除工作流",summary="删除工作流")
def delete_workflow(name: str):
    """
    删除工作流
    """
    pathlib.Path(f"data/{name}.json").unlink()
    return {"message": "success"}

@router.post("/execute_workflow",description="执行工作流",summary="执行工作流")
def execute_workflow(device: str, workflow: str):
    """
    执行工作流
    """
    return {"message": "success"}
